﻿using Net5ApiANdWpfClient.Lib.Common;
using Net5ApiANdWpfClient.Lib.Model;
using Net5ApiANdWpfClient.Lib.Model.SystemAdmin;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Net5ApiANdWpfClient.Lib.Service
{
    public class OutsourceService
    {
        /// <summary>
        /// 外协单位搜索方法
        /// </summary>
        /// <param name="loginName">登录名</param>
        /// <param name="fullName">全名</param>
        /// <param name="CurrentPageIndex">当前页</param>
        /// <returns></returns>
        public PagerResult<outsource> SearchPageOutSource(string os_name, string os_tel, int CurrentPageIndex)
        {
            string apiUrl = "api/Outsource/GetPageList"; //请求api的路径
            SearchModel search = new SearchModel()
            {
                SearchItem = new Dictionary<string, string> { { "os_name", os_name }, { "os_tel", os_tel } },
                page = CurrentPageIndex,
                limit = PcConfig.PageSize
            };
            ResultModel result = HttpRequestTools.PostByToken(apiUrl, JsonConvert.SerializeObject(search));
            if (result.code == ResultCode.Ok)
            {
                return JsonConvert.DeserializeObject<PagerResult<outsource>>(JsonConvert.SerializeObject(result.data));
            }
            else
            {
                return null;
            }

        }
        /// <summary>
        /// 外协单位删除（单删）
        /// </summary>
        /// <param name="os_id"></param>
        /// <returns></returns>
        public bool DeleteOutsource(int os_id)
        {
            string apiUrl = "api/Outsource/DeleteOutsourcePo?os_id="+os_id;//请求api路径
            ResultModel result = HttpRequestTools.PostByToken(apiUrl);
            if (result.code==ResultCode.Ok)
            {
                return true                 ;  
            }
            else
            {
                return false;
            }

        }
        /// <summary>
        /// 外协单位的添加
        /// </summary>
        /// <param name="os"></param>
        /// <returns></returns>
        public bool AddOutsource(outsource os,out string mess)
        {
            if (os==null)
            {
                mess = "数据错误！请确认是否填写正确!";
                return false;
            }
            string apiUrl = "api/Outsource/AddOutsourcePo";//请求api路径
            ResultModel result = HttpRequestTools.PostByToken(apiUrl,JsonConvert.SerializeObject(os));
            mess = result.mess;
            if (result.code ==ResultCode.Ok)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 外协单位反填数据
        /// </summary>
        /// <param name="os_id"></param>
        /// <returns></returns>
        public ResultModel EditOutsource(int os_id)
        {
            string apiUrl = "api/Outsource/GetOutsourcePoID?os_id=" + os_id;
            ResultModel result = HttpRequestTools.PostByToken(apiUrl);
            if (result.code==ResultCode.Ok)
            {
                return result;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 修改外协单位
        /// </summary>
        /// <param name="os"></param>
        /// <param name="mess"></param>
        /// <returns></returns>
        public bool UpdateOutSource(outsource os,out string mess)
        {
            string apiUrl = "api/Outsource/UpdateOutsourcePo";
            ResultModel result = HttpRequestTools.PostByToken(apiUrl,JsonConvert.SerializeObject(os));
            mess = result.mess;
            if (result.code==ResultCode.Ok)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
